<?php
namespace app\index\command;

use service\TreeService;
use service\FeishuService;
use think\console\Command;
use think\console\Input;
use think\console\Output;
use think\Db;
use think\Request;

use service\GuardService;

class CronGuard extends Command
{
    /**
     * 基础配置信息
     */
    protected function configure()
    {
        parent::configure();

        $this->setName('cronguard')
            ->setDescription('更新门禁系统部门或员工')
            ->setHelp('php think cronguard 更新类型（1部门  2员工）；')
            ->addArgument('utype');
    }

    /**
     * 执行体
     * @param Input $input
     * @param Output $output
     * @return int|void|null
     */
    protected function execute(Input $input, Output $output)
    {
        $utype = $input->getArgument('utype');
        if (empty($utype)) {
            $utype = 1;
        }
        if (!empty($utype) && !in_array($utype, [1, 2])) {
            exit("参数错误！1代表部门更新，2代表员工更新" . PHP_EOL);
        }
        $starttime = explode(' ',microtime());
        echo "start".PHP_EOL;
        $ctime = time();
        $main_department_lists = Db::name("department")
            ->where(['status'=>1])
            ->order("pid asc,sort asc")->select();
        //dump($main_department_lists);exit;
        $depids = [];
        //绿城中国控股有限公司+其他-外协单位-数字化建设中心服务团队
        //10000000 绿城中国控股有限公司
        //99990176 其他
        //99900001 外协单位
        //94001249 数字化建设中心服务团队
        $depids[] = $mainroot = 10000000;
        $treelists = TreeService::listToTree($main_department_lists,'department_id', $pid = 'pid', $child = '_child', $root = $mainroot);
        //dump(count($treelists));
        $childlists = tree_to_list($treelists,'_child');
        //dump(count($childlists));
        $depids = array_merge($depids,array_column($childlists,'department_id'));
        //dump(count($depids));exit;
        $depids[] = $other = 99990176;
        $depids[] = $wxdw = 99900001;
        $depids[] = $mainroot = 94001249;
        $treelists = TreeService::listToTree($main_department_lists,'department_id', $pid = 'pid', $child = '_child', $root = $mainroot);
        //dump(count($treelists));
        $childlists = tree_to_list($treelists,'_child');
        //dump(count($childlists));
        $depids = array_merge($depids,array_column($childlists,'department_id'));
        //dump(count(array_unique($depids)));
        $main_department_lists = Db::name("department")
            ->where(['department_id'=>['in',$depids]])
            ->select();
        //dump(count($main_department_lists));exit;
        if($utype == 1){
            //dump(count($main_department_lists));exit;
            foreach ($main_department_lists as $main_department_list){
                if($main_department_list['pid'] == 0){
                    $main_department_list['pid'] = 1;
                }
                $res = GuardService::instance()->AddDepartment($main_department_list);
                $u_data = [];
                if($res['code'] == 0 && $res['message'] == 'success'){
                    $u_data['update_guard_state'] = 1;
                }
                else{
                    $u_data['update_guard_state'] = 2;
                }
                $u_data['update_guard_res'] = json_encode($res,304);
                Db::name("department")->where(['id'=>$main_department_list['id']])->update($u_data);
            }
        }
        else{
            //$departmentlists = Db::name("user")->select();
            $departmentlists = $main_department_lists;
            $dcount = 1;
            foreach ($departmentlists as $departmentlist){
                echo $dcount++."=========================================".PHP_EOL;
                FeishuService::instance()->GetuserBydeparmentid($departmentlist['department_id'],$ctime);
            }
            //Db::name("user")->where(['update_time'=>['<>',$ctime]])->update(['update_state'=>0,'is_jtzb'=>0]);
        }
        $endtime = explode(' ',microtime());
        $thistime = $endtime[0]+$endtime[1]-($starttime[0]+$starttime[1]);
        $thistime = round($thistime,3);
        echo "本次执行耗时：".$thistime." 秒。".PHP_EOL;
    }

}